/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */
 
package jdbcmanager.dbtools.datatypes;

import java.sql.Types;

/**
 * Converts from an SQL data type as defined in <code>java.sql.Types</code> 
 * to its string representation and viceversa.
 * 
 * @author Francisco Morero Peyrona
 */

public class JDBCTypeConverter
{
   /**
    * From SQL type to its string representation.
    * 
    * @param nJdbcType SQL type
    * @return The string representation
    */
   public static String getJdbcTypeName( int nJdbcType ) 
   {
      String sName = "OTHER";
      
      switch( nJdbcType ) 
      {
         case Types.ARRAY:         sName = "ARRAY";          break;
         case Types.BIGINT:        sName = "BIGINT";         break;
         case Types.BINARY:        sName = "BINARY";         break;
         case Types.BIT:           sName = "BIT";            break;
         case Types.BLOB:          sName = "BLOB";           break;
         case Types.BOOLEAN:       sName = "BOOLEAN";        break;
         case Types.CHAR:          sName = "CHAR";           break;
         case Types.CLOB:          sName = "CLOB";           break;
         case Types.DATALINK:      sName = "DATALINK";       break;
         case Types.DATE:          sName = "DATE";           break;
         case Types.DECIMAL:       sName = "DECIMAL";        break;
         case Types.DISTINCT:      sName = "DISTINCT";       break;
         case Types.DOUBLE:        sName = "DOUBLE";         break;
         case Types.FLOAT:         sName = "FLOAT";          break;
         case Types.INTEGER:       sName = "INTEGER";        break;
         case Types.JAVA_OBJECT:   sName = "JAVA_OBJECT";    break;
         case Types.LONGVARBINARY: sName = "LONGVARBINARY";  break;
         case Types.LONGVARCHAR:   sName = "LONGVARCHAR";    break;
         case Types.NULL:          sName = "NULL";           break;
         case Types.NUMERIC:       sName = "NUMERIC";        break;
         case Types.REAL:          sName = "REAL";           break;
         case Types.REF:           sName = "REF";            break;
         case Types.SMALLINT:      sName = "SMALLINT";       break;
         case Types.STRUCT:        sName = "STRUCT";         break;
         case Types.TIME:          sName = "TIME";           break;
         case Types.TIMESTAMP:     sName = "TIMESTAMP";      break;
         case Types.TINYINT:       sName = "TINYINT";        break;
         case Types.VARBINARY:     sName = "VARBINARY";      break;
         case Types.VARCHAR:       sName = "VARCHAR";        break;
      }

      return sName;
   }

   /**
    * From the string representation to the SQL data type.
    * 
    * @param sJdbcTypeName The data type as string
    * @return The correspondant SQL data type
    */
   public static int getJdbcType( String sJdbcTypeName )
   {
      int nType = Types.OTHER;
      
      sJdbcTypeName = sJdbcTypeName.trim().toUpperCase();

      if (      "ARRAY".equals( sJdbcTypeName ) )          nType = Types.ARRAY;
      else if ( "BIGINT".equals( sJdbcTypeName ) )         nType = Types.BIGINT;
      else if ( "BINARY".equals( sJdbcTypeName ) )         nType = Types.BINARY;
      else if ( "BIT".equals( sJdbcTypeName ) )            nType = Types.BIT;
      else if ( "BLOB".equals( sJdbcTypeName ) )           nType = Types.BLOB;
      else if ( "BOOLEAN".equals( sJdbcTypeName ) )        nType = Types.BOOLEAN;
      else if ( "CHAR".equals( sJdbcTypeName ) )           nType = Types.CHAR;
      else if ( "CLOB".equals( sJdbcTypeName ) )           nType = Types.CLOB;
      else if ( "DATALINK".equals( sJdbcTypeName ) )       nType = Types.DATALINK;
      else if ( "DATE".equals( sJdbcTypeName ) )           nType = Types.DATE;
      else if ( "DECIMAL".equals( sJdbcTypeName ) )        nType = Types.DECIMAL;
      else if ( "DISTINCT".equals( sJdbcTypeName ) )       nType = Types.DISTINCT;
      else if ( "DOUBLE".equals( sJdbcTypeName ) )         nType = Types.DOUBLE;
      else if ( "FLOAT".equals( sJdbcTypeName ) )          nType = Types.FLOAT;
      else if ( "INTEGER".equals( sJdbcTypeName ) )        nType = Types.INTEGER;
      else if ( "JAVA_OBJECT".equals( sJdbcTypeName ) )    nType = Types.JAVA_OBJECT;
      else if ( "LONGVARBINARY".equals( sJdbcTypeName ) )  nType = Types.LONGVARBINARY;
      else if ( "LONGVARCHAR".equals( sJdbcTypeName ) )    nType = Types.LONGVARCHAR;
      else if ( "NULL".equals( sJdbcTypeName ) )           nType = Types.NULL;
      else if ( "NUMERIC".equals( sJdbcTypeName ) )        nType = Types.NUMERIC;
      else if ( "REAL".equals( sJdbcTypeName ) )           nType = Types.REAL;
      else if ( "REF".equals( sJdbcTypeName ) )            nType = Types.REF;
      else if ( "SMALLINT".equals( sJdbcTypeName ) )       nType = Types.SMALLINT;
      else if ( "STRUCT".equals( sJdbcTypeName ) )         nType = Types.STRUCT;
      else if ( "TIME".equals( sJdbcTypeName ) )           nType = Types.TIME;
      else if ( "TIMESTAMP".equals( sJdbcTypeName ) )      nType = Types.TIMESTAMP;
      else if ( "TINYINT".equals( sJdbcTypeName ) )        nType = Types.TINYINT;
      else if ( "VARBINARY".equals( sJdbcTypeName ) )      nType = Types.VARBINARY;
      else if ( "VARCHAR".equals( sJdbcTypeName ) )        nType = Types.VARCHAR;
       
      return nType;
   }
   
   /**
    * Return <code>true</code> if it corresponds with <code>sql.Types.OTHER</code> type or none of types described in the class
    * 
    * @param nJdbcType Type to check
    * @return <code>true</code> if it corresponds with <code>sql.Types.OTHER</code> type or none of types described in the class
    */
   public static boolean isDbSpecific( int nJdbcType )
   {
      return getJdbcTypeName( nJdbcType ).equals( "OTHER" );
   }
   
   /**
    * Return <code>true</code> if it corresponds with <code>sql.Types.OTHER</code> type or none of types described in the class
    * 
    * @param sJdbcType Type to check
    * @return <code>true</code> if it corresponds with <code>sql.Types.OTHER</code> type or none of types described in the class
    */
   public static boolean isDbSpecific( String sJdbcType )
   {
      return getJdbcType( sJdbcType ) == Types.OTHER;
   }
}